﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace SFJDService
{
    /// <summary>
    /// Win_Debug.xaml 的交互逻辑
    /// </summary>
    public partial class Win_Debug : Window
    {
        //private RichTextBox richText = null;
        private ListBox displaybox;

        private LogBoxBase logBox = new LogBoxBase();
        private ScrollViewer scroll;
        private FlowDocument richDoc = new FlowDocument();

        public Win_Debug()
        {
            InitializeComponent();
            Closed += Win_Debug_Closed;
            //Ini();
            start();
        }

        private void Win_Debug_Closed(object sender, EventArgs e)
        {
            Runtime.DebugWindowSwitch();
            //Close();
        }

        private void Ini()
        {
            Title = "司法鉴定助手-调试";
            //richText = new RichTextBox()
            //{
            //    IsReadOnly = true, MinWidth = 500, MinHeight = 400, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
            //};
            //richText.TextChanged += (s, e) => { richText.ScrollToEnd(); };
            //Layout_Root.Children.Add(richText);

            displaybox = new ListBox
            {
                Margin = new Thickness(10, 0, 10, 0),
            };
            Layout_Root.Children.Add(displaybox);
        }

        private void start()
        {
            Title = "司法鉴定助手-调试";
            scroll = new ScrollViewer
            {
                HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
            };
            scroll.Content = logBox;
            Content = scroll;
        }

        public void OutputMsg(string str)
        {
            //Dispatcher.Invoke(new Action(delegate
            //{
            //    //var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签  
            //    //var r = new System.Windows.Documents.Run(str); // Run 是一个 Inline 的标签
            //    //p.Inlines.Add(r);
            //    //p.LineHeight = 1;
            //    //p.Foreground = Brushes.Black;//设置字体颜色
            //    //richDoc.Blocks.Add(p);
            //    //richText.Document = richDoc;
            //    //richText.AppendText(str + "\r");
            //    //richText.AppendText(Environment.NewLine);
            //    //richText.Focus();

            //    displaybox.Items.Add(new ListBoxItem { Content = str });
            //}));
            logBox.WriteLine(str);
            Dispatcher.Invoke(new Action(() =>
            {
                scroll.ScrollToBottom();
            }));
        }
    }
}
